// Copyright 2018 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

package com.aiwu.memory;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentTransaction;

import com.aiwu.library.util.DisplayUtil;

import org.citra.citra_emu.R;

/**
 * 内存查看器
 */
public final class MemoryDialogActivity extends FragmentActivity implements View.OnClickListener, MemoryActView, OnActionCallback {

    private ViewGroup layoutContainer, layoutTitle;
    private View btnReset, btnClose;
    private FrameLayout layoutFragment;

    private MemoryFirstFragment memoryFirstFragment;
    private MemoryLoadingFragment memoryLoadingFragment;
    private MemoryResultManyFragment memoryResultManyFragment;
    private MemoryResultListFragment memoryResultListFragment;
    private MemoryNormalBackGameFragment memoryBackGameFragment;
    private MemoryFuzzyBackGameFragment memoryFuzzyBackGameFragment;
    private KeyboardFragment keyboardFragment;

    public static void launch(Context context) {
        Intent intent = new Intent(context, MemoryDialogActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_memory_dialog);
        initView();
        setAttributes();
        MemoryManager.getInstance().setMemoryActView(this);
        MemoryManager.getInstance().addOnActionCallback(this);
        restoreStep();
    }

    @Override
    protected void onDestroy() {
        MemoryManager.getInstance().removeMemoryActView();
        MemoryManager.getInstance().removeOnActionCallback(this);
        super.onDestroy();
    }

    private void restoreStep() {
        MemoryManager.getInstance().restoreStep();
    }

    @Override
    public void showFirstFragment() {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.layout_fragment_center, getMemoryFirstFragment())
                .show(getMemoryFirstFragment())
                .commitAllowingStateLoss();
    }

    @Override
    public void showLoadingFragment() {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.layout_fragment_center, getMemoryLoadingFragment())
                .show(getMemoryFirstFragment())
                .commitAllowingStateLoss();
    }

    @Override
    public void showResultManyFragment() {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.layout_fragment_center, getMemoryResultManyFragment())
                .show(getMemoryResultManyFragment())
                .commitAllowingStateLoss();
    }

    @Override
    public void showResultListFragment() {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.layout_fragment_center, getMemoryResultListFragment())
                .show(getMemoryResultListFragment())
                .commitAllowingStateLoss();
    }

    @Override
    public void showNormalBackGameFragment() {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.layout_fragment_center, getNormalBackGameFragment())
                .show(getNormalBackGameFragment())
                .commitAllowingStateLoss();
    }

    @Override
    public void showFuzzyBackGameFragment() {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.layout_fragment_center, getFuzzyBackGameFragment())
                .show(getFuzzyBackGameFragment())
                .commitAllowingStateLoss();
    }

    @Override
    public void showKeyboardFragment(boolean search) {
        Bundle arg = new Bundle();
        arg.putBoolean(KeyboardFragment.EXTRA_SEARCH, search);
        getKeyboardFragment().setArguments(arg);
        if (DisplayUtil.isPortrait()) {
            FrameLayout layoutKeyboard = findViewById(R.id.layout_fragment_keyboard);
            RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) layoutKeyboard.getLayoutParams();
            lp.setMargins(lp.leftMargin, search ? DisplayUtil.dp2px(144f) : DisplayUtil.dp2px(104f), lp.rightMargin, lp.bottomMargin);
            layoutKeyboard.setLayoutParams(lp);
        }
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.layout_fragment_keyboard, getKeyboardFragment())
                .show(getKeyboardFragment())
                .commitAllowingStateLoss();
    }

    @Override
    public void hideKeyboardFragment() {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.hide(getKeyboardFragment())
                .commitAllowingStateLoss();
    }

    @Override
    public void finishAct() {
        finish();
    }

    @Override
    public KeyboardFragment getKeyboardFragment() {
        if (keyboardFragment == null) {
            keyboardFragment = new KeyboardFragment();
        }
        return keyboardFragment;
    }

    @Override
    public MemoryLoadingFragment getMemoryLoadingFragment() {
        if (memoryLoadingFragment == null) {
            memoryLoadingFragment = new MemoryLoadingFragment();
        }
        return memoryLoadingFragment;
    }

    @Override
    public MemoryResultManyFragment getMemoryResultManyFragment() {
        if (memoryResultManyFragment == null) {
            memoryResultManyFragment = new MemoryResultManyFragment();
        }
        return memoryResultManyFragment;
    }

    @Override
    public MemoryResultListFragment getMemoryResultListFragment() {
        if (memoryResultListFragment == null) {
            memoryResultListFragment = new MemoryResultListFragment();
        }
        return memoryResultListFragment;
    }

    @Override
    public MemoryFirstFragment getMemoryFirstFragment() {
        if (memoryFirstFragment == null) {
            memoryFirstFragment = new MemoryFirstFragment();
        }
        return memoryFirstFragment;
    }

    @Override
    public MemoryNormalBackGameFragment getNormalBackGameFragment() {
        if (memoryBackGameFragment == null) {
            memoryBackGameFragment = new MemoryNormalBackGameFragment();
        }
        return memoryBackGameFragment;
    }

    @Override
    public MemoryFuzzyBackGameFragment getFuzzyBackGameFragment() {
        if (memoryFuzzyBackGameFragment == null) {
            memoryFuzzyBackGameFragment = new MemoryFuzzyBackGameFragment();
        }
        return memoryFuzzyBackGameFragment;
    }

    private void initView() {
        layoutContainer = findViewById(R.id.layout_container);
        layoutTitle = findViewById(R.id.layout_title);
        layoutFragment = findViewById(R.id.layout_fragment_center);
        btnReset = findViewById(R.id.btn_reset);
        btnReset.setOnClickListener(this);
        btnClose = findViewById(R.id.btn_close);
        btnClose.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_reset:
                MemoryManager.getInstance().reset();
                break;
            case R.id.btn_close:
                finish();
                break;
        }
    }

    private void setAttributes() {
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.MATCH_PARENT;
        getWindow().setAttributes(params);
    }

    @Override
    public void onShowKeyboard() {
        if (DisplayUtil.isLandscape()) {
            layoutTitle.setBackgroundResource(R.drawable.dialog_title_left);
        }
    }

    @Override
    public void onCloseKeyboard() {
        if (DisplayUtil.isLandscape()) {
            layoutTitle.setBackgroundResource(R.drawable.dialog_title);
        }
    }
}
